package com.cloud2eye.sys.controller;


import com.cloud2eye.base.http.ReqParams;
import com.cloud2eye.base.http.Result;
import com.github.crab2died.ExcelUtils;
import com.github.pagehelper.Page;
import com.cloud2eye.sys.service.RoleResourceService;
import com.cloud2eye.sys.model.RoleResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;


@RestController
@RequestMapping("/roleResource")
public class RoleResourceController {

    @Autowired
    private RoleResourceService roleResourceService;

    @RequestMapping("/save")
    public Result saveRoleResource(@RequestBody RoleResource roleResource) {

        roleResourceService.save(roleResource);
        return new Result().success(roleResource);
    }

    @RequestMapping("/delete")
    public Result deleteroleResource(@RequestBody String id) {
        roleResourceService.deleteById(id);
        return new Result().success();
    }

    @RequestMapping("/update")
    public Result updateRoleResource(@RequestBody RoleResource RoleResource) {

        roleResourceService.update(RoleResource);
        return new Result().success();
    }

    @RequestMapping("/pageQuery")
    public Result pageQuery(@RequestBody ReqParams<RoleResource> params) {
        Page<RoleResource> page = roleResourceService.pageQueryLike(params.getData(), params.getPageNum(), params.getPageSize(), "");
        return new Result().success(page);
    }

    @RequestMapping("/queryAll")
    public Result queryList(@RequestBody RoleResource RoleResource) {
        List<RoleResource> list = roleResourceService.queryAll(RoleResource);
        return new Result().success(list);
    }

    @RequestMapping("/detail")
    public Result queryDetail(@RequestBody String id) {
        RoleResource RoleResource = roleResourceService.queryById(id);
        return new Result().success(RoleResource);
    }

    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, @RequestBody ReqParams<RoleResource> params) {
        List<RoleResource> list = null;
        if (params.getPageNum() == 0) {
            list = roleResourceService.queryAll(new RoleResource());
        } else {
            Page<RoleResource> page = roleResourceService.pageQuery(params.getData(), params.getPageNum(), params.getPageSize(), "RoleResource_no");
            list = page.getResult();
        }
        try {
            long fileName = System.currentTimeMillis();
            response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");
            OutputStream outputStream = response.getOutputStream();
            ExcelUtils.getInstance().exportObjects2Excel(list, RoleResource.class, true, "sheet1", true, outputStream);
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
